#ifndef TSERIALPORT_H
#define TSERIALPORT_H

#include <QString>
#include <QThread>
#include <QSerialPort>
#include <QDebug>
#include <unistd.h>
#include <QDateTime>
#include <QMutex>
#include <QTimer>


class TserialPort : public QObject
{
    Q_OBJECT
public:
    explicit TserialPort(QObject *parent = nullptr);
    ~TserialPort();
    void openPort(QString name
                  ,QSerialPort::BaudRate baudRate
                  ,QSerialPort::DataBits dataBits
                  ,QSerialPort::Parity parity
                  ,QSerialPort::StopBits stopBits);
    void closePort();
    void writeData(QByteArray data);
signals:
    void serialStateChanged(const int state,const QString portName,const QString info);
    void serialRevReport(QByteArray readBuf);
private slots:
    void on_serialPort_ready();
    void on_errorOccurred(QSerialPort::SerialPortError error);

private:
    QString portName;

    QSerialPort *comPort;
    QThread *thread;
    QDateTime *dateTime;
    QMutex *mutex;
    qint8 revCnt=0;
    QTimer *timer;
    QByteArray readBuf;
};

#endif // TSERIALPORT_H
